<script>

for(var i=0; i<4; i++){
    setTimeout((function (i){
        console.log(i)
    })(i), 0)
}

</script>